package com.bawei.module_home.repo

import com.bawei.module_home.api.HomeApiServer
import com.bawei.module_home.entriy.CommendEntity
import com.bawei.module_home.entriy.CommentListEntriyItem
import com.bawei.module_home.entriy.SimpleTypeBeanItem
import com.bawei.module_home.entriy.SimpleVideoEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okhttp3.RequestBody
import javax.inject.Inject

class HomeRepository @Inject constructor(private val apiserver:HomeApiServer) {
    suspend fun getSimpleType():Flow<List<SimpleTypeBeanItem>>{
        return flow {
            val result=apiserver.getSimpleType()
            if(result.code==200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    suspend fun getRecommendSimpleVideoList(pageIndex: Int, pageSize: Int): Flow<List<SimpleVideoEntity>>{
        return flow {
            var result = apiserver.getRecommendSimpleVideo(pageIndex, pageSize)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    suspend fun getSimpleVideoList(channelId: String, pageIndex: Int, pageSize: Int): Flow<List<SimpleVideoEntity>>{
        return flow {
            var result = apiserver.getSimpleVideoByChannelId(channelId, pageIndex, pageSize)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    suspend fun getVideoCommends(itemId: String, dataType: Int): Flow<List<CommentListEntriyItem>>{
        return flow {
            var result = apiserver.getVideoCommends(dataType, itemId)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }

    suspend fun publishVideoComment(requestBody: RequestBody): Flow<List<CommendEntity>>{
        return flow {
            var result = apiserver.publishVideoComment(requestBody)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(emptyList())
            }
        }
    }
}